﻿@page "/fetchdata"
@using DynamicQuartz.Schedule.Core
@inject QuartzService quartzService
@inject IJSRuntime JSRuntime
@using System.Timers

<PageTitle>定时任务</PageTitle>

<h1>定时任务</h1>

<p>后台定时任务</p>
@if (jobs == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>任务组名称</th>
                <th>任务名称</th>
                <th>任务名称</th>
                <th>开始执行时间</th>
                <th>上次执行时间</th>
                <th>状态</th>
                <th>执行计划</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var job in jobs)
            {
                <tr>
                    <td>@job.JobGroupName</td>
                    <td>@job.JobName</td>
                    <td>@job.JobDescription</td>
                    <td>@job.StartTime</td>
                    <td>@job.PreviousFireTime</td>
                    <td>@job.DisplayState</td>
                    <td>@job.ScheduleExpression</td>
                    <td>
                        <button class="btn btn-primary btn-sm" @onclick="() => ExecuteJob(job)">立即执行</button>
                        &nbsp;
                        @if (job.DisplayState == "暂停")
                        {
                            <button class="btn btn-success btn-sm" @onclick="() => ResumeJob(job)"><span style="color:#fff">恢复</span></button>
                        }
                        @if (job.DisplayState == "正常")
                        {
                            <button class="btn btn-warning btn-sm" @onclick="() => SuspendJob(job)"><span style="color:#fff">暂停</span></button>
                        }
                    </td>
                </tr>
            }
        </tbody>
    </table>
}

@code {
    private Timer refreshTimer; // 定时器
    private string lastRefreshTime = "尚未刷新"; // 最后刷新时间
    private List<JobDetail> jobs;
    protected override async Task OnInitializedAsync()
    {
        await GetListAsync();
    }
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            // 初始化定时器，设置间隔为 30 秒（30000 毫秒）
            refreshTimer = new Timer(30000);
            refreshTimer.Elapsed += RefreshData; // 绑定事件
            refreshTimer.AutoReset = true; // 设置为重复触发
            refreshTimer.Start(); // 启动定时器
        }
    }
    private async Task ExecuteJob(JobDetail job)
    {
        await quartzService.ImmediatelyExecuteJob(new Quartz.JobKey(job.JobName));
        await GetListAsync();
    }
    private async Task ResumeJob(JobDetail job)
    {
        await quartzService.ResumeJob(new Quartz.JobKey(job.JobName));
        await GetListAsync();
    }

    private async Task SuspendJob(JobDetail job)
    {
        await quartzService.SuspendJob(new Quartz.JobKey(job.JobName));
        await GetListAsync();
    }

    private async Task GetListAsync()
    {
        jobs = await quartzService.GetAllJobs();
    }
    private async void RefreshData(object sender, ElapsedEventArgs e)
    {
        // 更新最后刷新时间
        lastRefreshTime = DateTime.Now.ToString("HH:mm:ss");

        // 通知 Blazor 重新渲染 UI
        await InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        // 释放定时器资源
        refreshTimer?.Stop();
        refreshTimer?.Dispose();
    }
}
